package com.job.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.common.ResponseResult;
import com.job.common.asserts.MyAssert;
import com.job.common.converter.CorporationFieldConverter;
import com.job.common.converter.PositionFieldConverter;
import com.job.common.enums.ResultCodeEnum;
import com.job.common.exception.AddCorporationException;
import com.job.common.exception.ParameterIsNullException;
import com.job.controller.req.CorporationInfoVO;
import com.job.controller.req.CorporationSearchVO;
import com.job.controller.req.HrCorporationVo;
import com.job.controller.resp.CorporationInfoRespVO;
import com.job.dao.CorporationDao;
import com.job.dao.PositionDao;
import com.job.mapper.CorporationInfoMapper;
import com.job.mapper.CorporationMapper;
import com.job.pojo.Corporation;
import com.job.pojo.CorporationInfo;
import com.job.pojo.Position;
import com.job.pojo.vo.CorporationVO;
import com.job.pojo.vo.PositionVO;
import com.job.service.CorporationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CorporationServiceImpl implements CorporationService {

    @Resource
    private CorporationMapper corporationMapper;

    @Resource
    private CorporationInfoMapper corporationInfoMapper;

    @Resource
    private CorporationDao corporationDao;

    @Resource
    private PositionDao positionDao;

    @Resource
    private CorporationFieldConverter corporationFieldConverter;

    @Resource
    private PositionFieldConverter positionFieldConverter;

    @Override
    public ResponseResult<Page<CorporationVO>> corporationSearch(CorporationSearchVO corporationSearchVO)
            throws ParameterIsNullException {
        MyAssert.objectParameterCheck(corporationSearchVO);

        corporationSearchVO.setCurrent((corporationSearchVO.getCurrent()-1) * corporationSearchVO.getSize());
        int count = corporationMapper.searchCorporationsCount(corporationSearchVO);
        List<Corporation> corporations = corporationMapper.searchCorporations(corporationSearchVO);
        if (CollectionUtils.isEmpty(corporations)){
            log.info("不存在你描述的公司");
            new ResponseResult<Corporation>().fail(ResultCodeEnum.CORPORATION_IS_NULL_FAIL);
        }
        List<CorporationVO> corporationVOS = new ArrayList<>();
        corporations.forEach(corporation -> {
            corporationVOS.add(corporationFieldConverter.corporationPoToVoConverter(corporation));
        });

        Page<CorporationVO> page = new Page<>();
        page.setCurrent(corporationSearchVO.getCurrent());
        page.setSize(corporationSearchVO.getSize());
        page.setTotal(count);
        page.setRecords(corporationVOS);
        return new ResponseResult<Page<CorporationVO>>().success(page);
    }

    @Override
    public ResponseResult<CorporationInfoRespVO> corporationInfoContext(CorporationInfoVO corporationInfoVO)
            throws ParameterIsNullException {
        MyAssert.objectParameterCheck(corporationInfoVO);
        Corporation corporation = corporationMapper.selectById(corporationInfoVO.getCorporationId());
        if (Objects.isNull(corporation)){
            log.info("该公司信息不存在");
            new ResponseResult<Corporation>().fail(ResultCodeEnum.CORPORATION_INFO_IS_NULL_FAIL);
        }
        //查询公司详情和岗位信息
        CorporationInfo corporationInfo = corporationDao.queryCorporationInfo(corporationInfoVO.getCorporationId());
        Page<Position> positions = positionDao.queryPositionByCorporationId(corporationInfoVO);
        ArrayList<PositionVO> positionVOS = new ArrayList<>();
        positions.getRecords().forEach(position -> {
            positionVOS.add(positionFieldConverter.PositionPoToVoConverter(position));
        });
        Page<PositionVO> positionVoPage = new Page<>();
        BeanUtils.copyProperties(positions,positionVoPage);
        positionVoPage.setRecords(positionVOS);

        CorporationInfoRespVO corporationInfoRespVO = new CorporationInfoRespVO();
        corporationInfoRespVO.setCorporationVO(corporationFieldConverter.corporationPoToVoConverter(corporation));
        corporationInfoRespVO.setCorporationInfo(corporationInfo);
        corporationInfoRespVO.setPositions(positionVoPage);
        return new ResponseResult<CorporationInfoRespVO>().success(corporationInfoRespVO);
    }

    @Transactional(rollbackFor = AddCorporationException.class)
    @Override
    public ResponseResult<ResultCodeEnum> corporationAdd(HrCorporationVo hrCorporationVo)
            throws AddCorporationException {
        Corporation corporation = new Corporation();
        CorporationInfo corporationInfo = new CorporationInfo();
        BeanUtils.copyProperties(hrCorporationVo,corporation);

        if (corporationMapper.insert(corporation) < 1){
            throw new AddCorporationException("插入数据失败");
        }
        corporationInfo.setCorporationId(corporation.getCorporationId());
        corporationInfo.setContext(hrCorporationVo.getContext());
        if (corporationInfoMapper.insert(corporationInfo) < 1){
            throw new AddCorporationException("插入数据失败");
        }
        return new ResponseResult<ResultCodeEnum>().success();
    }
}
